package com.feibi.firstplugin.Action;

import com.feibi.firstplugin.Bean.ProjectsResult;
import com.feibi.firstplugin.Bean.WordData;
import com.feibi.firstplugin.ContentUtil;
import com.feibi.firstplugin.Global;
import com.feibi.firstplugin.Utils.NotifyUtil;
import com.feibi.firstplugin.Utils.ResultCallback;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBScrollPane;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import java.awt.*;
import java.awt.event.ItemEvent;

public class InputJsonDialog extends DialogWrapper {
    JLabel duLabel;
    Project project;
    VirtualFile virtualFile;

    protected InputJsonDialog(@Nullable Project project, VirtualFile virtualFile) {
        super(project);
        setTitle("转繁体 —— Design By 亦心");
        init();
        this.project = project;
        this.virtualFile = virtualFile;
    }

    private JComboBox<ProjectsResult.ProjectData> createComboBox() {
        JComboBox<ProjectsResult.ProjectData> comboBox = new JComboBox<>();
        Dimension comboBoxDimension = new Dimension(200, comboBox.getPreferredSize().height);
        comboBox.setMaximumSize(comboBoxDimension);
        comboBox.addItem(Global.normalProject);
        if (Global.projectLists != null) {
            for (int i = 0; i < Global.projectLists.size(); i++) {
                comboBox.addItem(Global.projectLists.get(i));
            }
        }
        comboBox.setRenderer((list, value, index, isSelected, cellHasFocus) -> new JLabel(value.getDictLabel()));
        comboBox.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                ProjectsResult.ProjectData itemPro = (ProjectsResult.ProjectData) (e.getItem());
                if (!itemPro.getDictType().equals("0")) itemPro.setDictType("");
                ContentUtil.getWords(itemPro.getDictType(), itemPro.getDictValue(), results -> {
                    System.out.println("成功获取项目word数据");
                    Global.wordResults = results;
                    list.setModel(new ListModel<WordData.Result>() {
                        @Override
                        public int getSize() {
                            return results.size();
                        }

                        @Override
                        public WordData.Result getElementAt(int index) {
                            return results.get(index);
                        }

                        @Override
                        public void addListDataListener(ListDataListener l) {

                        }

                        @Override
                        public void removeListDataListener(ListDataListener l) {

                        }
                    });
                });
            }
        });
        return comboBox;
    }

    JBList<WordData.Result> list;

    private void createWordListPanel() {
        Global.wordResults = Global.normalWordResults;
        list = new JBList<>(Global.normalWordResults);
        // 设置单选模式
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // 设置渲染器，可以使用系统默认的或自定义的
        list.setCellRenderer((list1, value, index, isSelected, cellHasFocus) -> {
            JBLabel label = new JBLabel("　　" + value.getCnName() + " -> " + value.getTwName());
//            JCheckBox checkBox = new JCheckBox("　　" + value.getCnName() + " -> " + value.getTwName(), null, value.getSelected());
//            checkBox.addItemListener(e -> {
//                System.out.println(e.getStateChange());
//                if (e.getStateChange() == ItemEvent.SELECTED) {
//                    System.out.println("selectend");
//                    value.setSelected(true);
//                } else if (e.getStateChange() == ItemEvent.DESELECTED) {
//                    System.out.println("JCheckBox被取消选中了。");
//                    value.setSelected(false);
//                }
//            });
            return label;
        });
        // 添加选中事件的监听器
//        list.addListSelectionListener(e -> {
//            if (e.getValueIsAdjusting()) { // 防止多次处理
//                String selectedValue = list.getSelectedValue().getCnName(); // 获取选中项
//                System.out.println("Selected: " + selectedValue);
//            }
//        });
        JBScrollPane scrollPane = new JBScrollPane(list);
//            scrollPane.setPreferredSize(dimension);
//            scrollPane.setMaximumSize(dimension);
        mainPanel.add(scrollPane, BorderLayout.CENTER);
    }

    JPanel mainPanel = new JPanel();

    @Override
    protected @Nullable
    JComponent createCenterPanel() {
        Dimension dimension = new Dimension();
        dimension.setSize(800, 600);
        mainPanel.setPreferredSize(dimension);
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        JBLabel tipLabel = new JBLabel("在转换之前，建议先提交一次代码，之后可以清楚的知道改了哪些文件");
        mainPanel.add(tipLabel);
        mainPanel.add(new JBLabel(" "));
        mainPanel.add(createComboBox());
        if (Global.normalWordResults == null || Global.normalWordResults.isEmpty()) {
            System.out.println("没有通用word数据，先获取通用word数据");
            ContentUtil.getWords("0", "", results -> {
                System.out.println("成功获取通用word数据");
                Global.normalWordResults = results;
                createWordListPanel();
            });
        } else {
            System.out.println("有通用word数据，直接渲染");
            createWordListPanel();
        }
        return mainPanel;
    }

    @Override
    protected @Nullable
    JComponent createSouthPanel() {
        JPanel jPanel = new JPanel();
        JButton button = new JButton("开始转换");
        button.addActionListener(e -> {
            if (Global.wordResults == null || Global.wordResults.isEmpty()) {
                NotifyUtil.notify("没有获取到对照表，暂时无法转换");
            } else {
                WaitDialog dialog = new WaitDialog(project, Global.wordResults, virtualFile);
                dialog.show();
                this.dispose();
            }
        });
        jPanel.add(button);
        return jPanel;
    }
}
